<?php
namespace Admin\Controller;
/**
 * 基础控制器
 */
use Think\Controller;
class BaseController extends Controller {
	
	public function __construct(){
		parent::__construct();
		//初始化系统信息
		$m = D('Home/System');
		$GLOBALS['CONFIG'] = $m->loadConfigs();
		$this->assign('CONF',$GLOBALS['CONFIG']);
		$s = session('WST_STAFF');
		$this->admin = $s;
		$this->assign('WST_STAFF',$s);
	}
    
	/**
	 * 上传图片
	 */
    public function uploadPic(){
	   $config = array(
		        'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
		        'exts'          =>  array('jpg','png','gif','jpeg'), //允许上传的文件后缀
		        'rootPath'      =>  './Upload/', //保存根路径
		        'driver'        =>  'LOCAL', // 文件上传驱动
		        'subName'       =>  array('date', 'Y-m'),
		        'savePath'      =>  I('dir','uploads')."/"
		);
		$upload = new \Think\Upload($config);
		$rs = $upload->upload($_FILES);
		if(!$rs){
			$this->error($upload->getError());
		}else{
			$images = new \Think\Image();
			$images->open('./Upload/'.$rs['Filedata']['savepath'].$rs['Filedata']['savename']);
			$newsavename = str_replace('.','_thumb.',$rs['Filedata']['savename']);
			$vv = $images->thumb(I('width',300), I('height',300),I('thumb_type',1))->save('./Upload/'.$rs['Filedata']['savepath'].$newsavename);
		    if(C('WST_M_IMG_SUFFIX')!=''){
		        $msuffix = C('WST_M_IMG_SUFFIX');
		        $mnewsavename = str_replace('.',$msuffix.'.',$rs[$Filedata]['savename']);
		        $mnewsavename_thmb = str_replace('.',"_thumb".$msuffix.'.',$rs[$Filedata]['savename']);
			    $images->open('./Upload/'.$rs[$Filedata]['savepath'].$rs[$Filedata]['savename']);
			    $images->thumb(I('width',700), I('height',700))->save('./Upload/'.$rs[$Filedata]['savepath'].$mnewsavename);
			    $images->thumb(I('width',250), I('height',250))->save('./Upload/'.$rs[$Filedata]['savepath'].$mnewsavename_thmb);
			}
			$rs['Filedata']['savepath'] = "Upload/".$rs['Filedata']['savepath'];
			$rs['Filedata']['savethumbname'] = $newsavename;
			$rs['status'] = 1;
			echo json_encode($rs);
		}	
    }
    /**
     * 上传文件
     * Enter description here ...
     */
    public function uploadFile(){
    	
    }
    /**
     * ajax操作登录验证
     */
    public function isAjaxLogin(){
    	$s = session('WST_STAFF');
    	if(empty($s))die("{status:-999,url:'toLogin'}");
    }
    /**
     * 登录操作验证
     */
    public function isLogin(){
    	$s = session('WST_STAFF');
        if(empty($s))$this->redirect("Index/toLogin");
    }
    /**
     * 跳转权限操作
     */
    public function checkPrivelege($grant){
    	$s = session('WST_STAFF.grant');
    	if(empty($s) || !in_array($grant,$s)){
    		$this->display("/noprivelege");exit();
    	}
    }
    public function checkAjaxPrivelege($grant){
    	$s = session('WST_STAFF.grant');
    	if(empty($s) || !in_array($grant,$s))die("{status:-998}");
    }
    
    /**
     * 返回所有参数
     */
    function WSTAssigns(){
    	$params = I();
    	$this->assign("params",$params);
    }
	/*
    *团购活动倒计时修改状态
    */
    public function changeGroupStatus(){
            //团购活动信息
            $goodsGroupModel = M('GoodsGroup');
            $sql = "select goodsId from __PREFIX__goods where goodsFlag=1 and isGroup=1";
            $sql.=" order by goodsId desc";
            $goods = $goodsGroupModel->query($sql);
            //团购订单商品信息
            $ordersGoodsModel = M('OrderGoods');
            foreach($goods as $k=>$v){
                $sql = '';
                $group = $goodsGroupModel->where('goodsId ='.$v['goodsId'].' and groupStatus in(0,1)')->select();
                foreach($group as $ks=>$vs){
                    $orderGoods = $ordersGoodsModel->where('goodsGroupId = '.$vs['id'])->select();
                    //团购数量
                    $totalNums = 0;
                    foreach($orderGoods as $kss=>$vss){
                        $totalNums += $vss['goodsNums'];
                    }
                    //当团购活动结束时间小于当前时间（倒计时结束），修改活动状态
                        if($vs['endTime']<time()){
                            if($vs['groupMinCount']<=$totalNums){
                                $groupStatuss = 2;//成功结束
                            }else{
                                $groupStatuss = 3;//失败结束
                            }
                        }else{
                            if($vs['startTime']<time()){
                                $groupStatuss = 1;//进行中
                            }else{
                                $groupStatuss = 0;//未开始
                            }
                        }
                    $goodsGroupModel->where('id= '.$vs['id'])->setField('groupStatus',$groupStatuss);
                }
            } 
    }
    
}